package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.model.Vouchers;
import cn.edu.zjgsu.ito.service.IVoucherService;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.Page;

@Controller
@Transactional
@RequestMapping(value = "VoucherController")
public class VoucherController
{
	@Autowired
	private IVoucherService voucherService;

	/**
	 * 分页查看代金券,sequence为1按创建时间由新到旧,2按创建时间由旧到新,3查询未关闭领取的
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "vouchers/pageCount/{pageCount}/pageSize/{pageSize}",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getVouchers(@RequestParam(name="sequence",required=false) String sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*Page<Vouchers> page = voucherService.getVouchers(sequence,
				key == null ? "" : key, pageCount, pageSize);*/
		int sequenceInt = sequence==null?0:Integer.valueOf(sequence);
		List<Vouchers> vouchers = voucherService.getVouchersWithoutPaging(sequenceInt);
		/*if (page == null)
		{
			return error(result);
		} else
		{*/
			result.put("vouchers", vouchers);
			//result.put("count", page.getTotalCount());
			return success(result);
		//}
	}
	

	/**
	 * 增加代金券
	 * 
	 * @param voucher
	 * @return
	 */
	@RequestMapping(value = "voucher", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addVoucher(@RequestBody Vouchers voucher)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		System.out.println(voucher.toString());
		if (voucher.getVouchersName() == null
				|| voucher.getVouchersName().length() == 0
				|| voucher.getVouchersPrice() == null
				|| voucher.getVouchersPrice().length() == 0)
		{
			return error(result);
		}
		
		if (voucherService.addVoucher(voucher) != 1)
		{
			return error(result);
		} else
		{
			return success(result);
		}
	}
	
	/**
	 * 根据id取得代金券
	 * @param voucherId
	 * @return
	 */
	@RequestMapping(value = "voucher/{voucherId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getVoucher(@PathVariable int voucherId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("voucher", voucherService.getVoucherById(voucherId));
		return success(result);
	}
	

	/**
	 * 根据voucherId停止领取代金券
	 * 
	 * @param voucherId
	 * @return
	 */
	@RequestMapping(value = "voucher/{voucherId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> closeVoucher(@PathVariable int voucherId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		voucherService.closeVoucher(voucherId);
		return success(result);
	}

	/**
	 * 批量停止领取代金券
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "vouchers", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> closeVouchers(@RequestBody Map map)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<Integer> voucherIdsList = (List<Integer>) map.get("voucherIds");
		if (voucherIdsList == null || voucherIdsList.size() == 0)
		{
			return error(result);
		} else
		{
			for (int voucherId : voucherIdsList)
			{
				voucherService.closeVoucher(voucherId);
			}
		}
		return success(result);
	}
	
	
	/**
	 * 恢复代金券的领取
	 * @param voucherId
	 * @return
	 */
	@RequestMapping(value = "voucher/openVoucher/{voucherId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> openVoucher(@PathVariable int voucherId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		voucherService.openVoucher(voucherId);
		return success(result);
	}


	/**
	 * 修改代金券
	 * 
	 * @param voucher
	 * @return
	 */
	@RequestMapping(value = "voucher", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> updateVoucher(@RequestBody Vouchers voucher)
	{
		Map<String, Object> result = new HashMap<String, Object>();		
		if (voucher.getVouchersName() == null
				|| voucher.getVouchersName().length() == 0
				|| voucher.getVouchersPrice() == null
				|| voucher.getVouchersPrice().length() == 0)
		{
			return error(result);
		}
		
		voucherService.updateVoucher(voucher);
		return success(result);
	}
	
	

	private Map<String, Object> error(Map<String, Object> result)
	{
		result.put("code", Constant.FAIL);
		return result;
	}

	private Map<String, Object> success(Map<String, Object> result)
	{
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	/**
	 * 分页查看代金券,sequence为1按创建时间由新到旧,2按创建时间由旧到新,3查询未关闭领取的
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "vouchers/pageCount/{pageCount}/pageSize/{pageSize}",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getVouchers(@PathVariable int pageCount,
			@PathVariable int pageSize, @RequestParam int sequence,
			@RequestParam String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<Vouchers> page = voucherService.getVouchers(sequence,
				key == null ? "" : key, pageCount, pageSize);
		if (page == null)
		{
			return error(result);
		} else
		{
			result.put("vouchers", page.getData());
			result.put("count", page.getTotalCount());
			return success(result);
		}
	}*/
}
